﻿using System;
using System.Web.UI.WebControls;
using BusinessObject;
using Model;

namespace ViewLayer.Pages.Sprints
{
    public partial class CadastroSprint : System.Web.UI.Page
    {
        //private int i;
        private User user;

        private Project project;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)Session["User"];
            this.lblErro.Visible = false;
            this.lblErroRelease.Visible = false;

            if (user == null)
            {
                Response.Redirect("../Account/Login.aspx");
            }

            if (!IsPostBack)
            {
                if (Session["Project"] != null)
                {
                    this.project = Session["Project"] as Project;
                    ddlRelease.DataSource = ReleaseBO.Instance.GetReleaseByProject(project);
                    ddlRelease.DataTextField = "Nome";
                    ddlRelease.DataValueField = "Id";
                    ddlRelease.DataBind();

                    ddlRelease.Items.Add(new ListItem("Sem Associação", "0"));

                    ddlRelease.SelectedValue = "0";
                }
                //redireciona para a página de projetos
                else
                {
                    Response.Redirect("../Default.aspx");
                }
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            var inicio = DateTime.Parse(this.txtDataInicio.Text);
            var termino = DateTime.Parse(this.txtDataTermino.Text);

            if (this.ddlRelease.SelectedValue == "0")
            {
                this.lblErro.Visible = true;
            }
            else
            {
                if (SprintBO.Instance.CheckDates(ReleaseBO.Instance.GetById(int.Parse(this.ddlRelease.SelectedValue)),
                     inicio, termino))
                {
                    SprintBO.Instance.InsertSprint(ReleaseBO.Instance.GetById(int.Parse(this.ddlRelease.SelectedValue)), this.txtName.Text,
                        inicio, termino);

                    Response.Redirect("../Projetos/InicioProjeto.aspx");
                }
                else
                {
                    this.lblErro.Visible = true;
                }
            }
        }
    }
}